package eyrolles;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

import java.sql.SQLException;
import java.util.ArrayList;

public class EmployeListeAction extends ActionDB {

    protected ArrayList getEmployes() throws SQLException {

        Employe employe = null;
        ArrayList employes = new ArrayList();

        try {

            conn = getConnection();
            stmt = conn.createStatement();
            rs =
                    stmt.executeQuery("select * from employes, roles, "
                    + "services where employes.roleid=roles.roleid "
                    + "and employes.depid=services.depid");

            while (rs.next()) {

                employe = new Employe();

                employe.setUsername(rs.getString("username"));
                employe.setName(rs.getString("name"));
                employe.setRolename(rs.getString("rolename"));
                employe.setPhone(rs.getString("phone"));
                employe.setEmail(rs.getString("email"));
                employe.setRoleid(new Integer(rs.getInt("roleid")));
                employe.setDepid(new Integer(rs.getInt("depid")));
                employe.setDepartment(rs.getString("depname"));

                employes.add(employe);

            }

        } catch (NullPointerException e) {

            throw new SQLException();
        } finally {

            closeConnection();
        }

        return employes;
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        ActionMessages errors = new ActionMessages();

        // Redirection attendue si tout se passe bien
        String target = new String("success");

        // Teste si l'utilisateur est identifie
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas loggué
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));
            
            // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête
            return (mapping.findForward(target));
        }

        ArrayList employes = null;

        try {
            employes = getEmployes();
            request.setAttribute("employes", employes);

        } catch (SQLException e) {

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));
            target = new String("login");
        }

        // Signalement des erreurs eventuelles
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
        }

        // Forward to the appropriate View    
        return (mapping.findForward(target));
    }
}